<?php
/**
 *  Remove.php
 *
 *  @author    {$author}
 *  @package   Xanhte
 *  @version   $Id: Remove.php 163 2008-03-11 18:54:07Z mikhail.miguel $
 */

/**
 *  removeフォームの実装
 *
 *  @author    {$author}
 *  @access    public
 *  @package   Xanhte
 */
require_once dirname(__FILE__).".php";
class Xanhte_Form_XanhteFilemanagerRemove extends Xanhte_Form_XanhteFilemanager
{
    /**
     *  @access   private
     *  @var      array   フォーム値定義
     */
	 var $form = array(
		 'file_path' => array(),
		 'file_name' => array(),
		 );
	 
}

/**
 *  removeアクションの実装
 *
 *  @author     {$author}
 *  @access     public
 *  @package    Xanhte
 */
class Xanhte_Action_XanhteFilemanagerRemove extends Xanhte_Action_XanhteFilemanager
{
        /**
         *  removeアクションの前処理
         *
         *  @access    public
         *  @return    string  Forward先(正常終了ならnull)
         */
        function prepare()
        {
			// $file_pathの不正を除去1. $file_pathをregexp
			parent::prepare();
			return null;
        }

        /**
         *  removeアクションの実装
         *
         *  @access    public
         *  @return    string  遷移名
         */
        function perform()
        {
			
			$result = 0;
			$file_path = $this->af->get('file_path');
			$file_name = $this->af->get('file_name');
			
			$mModule =& $this->backend->getProcessModule();
			$c_handler =& xoops_gethandler('config');
			$xanhte_config = $c_handler->getConfigsByDirname($mModule->mXoopsModule->dirname());
			
			// $file_pathの不正を除去2. user_idを噛ましてopen_basedir相当の処理
			$target = sprintf('%s/%d/%s/%s',
							  $xanhte_config['iprop_upload_path'],
							  $this->backend->getUid(),
							  $file_path,
							  $file_name
							  );
			$path_target = XOOPS_ROOT_PATH.$target;
			if ($this->ae->count()==0){
				$is_dir = is_dir($path_target);
				$result = $this->removeFilesRecursive($path_target);
				/// DB上からも削除
				if ($result){
					$condition = $is_dir ? OBJECT_CONDITION_LIKE : OBJECT_CONDITION_EQ;
					$filter = array(
						'item' => new Ethna_AppSearchObject('xanhte_tinymce::'.$path_target, $condition),
						'itemid' => new Ethna_AppSearchObject($this->backend->getUid(), OBJECT_CONDITION_EQ),
						);
					$pict_m =& $this->getM('Picture');
					if (!Ethna::isError($ret = $pict_m->getObjectList('Picture', $filter))){
						foreach ($ret[1] as $p){
							$p->remove();
						}
					} else {
						echo $ret->getMessage();
					}
				}
			}
			$this->af->setApp('result', $result);
			
			return 'json';
        }
		
		
        /**
         *  再帰的にDirとFileを消していく
         *
         *  @access    protected
		 *  @param     string $targetとなるディレクトリまたはファイル(絶対パスで)
         *  @return    array  消去したDirまたはファイルの数
         */
        function removeFilesRecursive($target, &$ret=0)
        {
			if (file_exists($target)){
				if (is_dir($target)){
					$files = ITT_Util::getDirectoryList($target, false);
					foreach ($files as $f){
						$this->removeFilesRecursive($target."/".$f, $ret);
					}
					rmdir($target);
				} else {
					unlink($target);
					/// thumbnail
					$target_thumb = sprintf("%s/thumbnail%s", 
											substr($target,0, intval(strrpos($target, '/'))),
											strrchr($target,'/')
											);
					unlink($target_thumb);
					$ret++;
				}
			}
			return $ret;
		}		
}

